<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./css/animate.css">
    <title>Document</title>
    <style>
    th,td{
        border: 1px solid #000;
        text-align: center
    }
    </style>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>名称</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>总价</th>
                </tr>
            </thead>
            <tbody>
                <tr  v-for="(item,index) in info">
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                    
                    <td>
                        <button @click="del(index)">-</button>
                        {{item.num}}
                        <button @click="add(index)">+</button>
                    </td>
                   
                    <td>{{item.price*item.num}}</td>
                </tr>
            </tbody>
            <p>总价格:{{allprice}}</p>
        </table>


    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                info:[
                    {name:"苹果",price:8,num:0},
                    {name:"橘子",price:5,num:0},
                    {name:"西瓜",price:3,num:0}
                ],
                allprice:0

            },
        //    computed: {
        //        count(){
        //          return this.info.num*this.info.price
        //        }
               
          // },
           methods:{
               add(index){
                   this.info[index].num++,
                   this.sum()
               },
               del(index){
                   if(this.info[index].num==0){
                       return;
                   }
                   this.info[index].num--,
                   this.sum()
               },
               sum(){
                   var allprice=0;
                   this.info.forEach((item,index) => {
                       console.log(item); //{name:"苹果",price:8,num:0},
                        allprice+=item.price*item.num
                        
                   });
                   this.allprice=allprice

               }
           }
        })
    
    
    </script>
    
</body>
</html>